package cn.itcast.service.impl;

import cn.itcast.dao.MemberDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import cn.itcast.utils.MD5Utils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    // 根据手机号查询会员信息
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        //其它界面登录,进行密码加密(根据短信验证码登入的没有密码)
        if(member.getPassword() != null){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        //开启分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //根据条件分页查询会员档案
        Page<Member> page = memberDao.selectByCondition(queryPageBean.getQueryString());
        //构建分页数据返回
        return new PageResult(page.getTotal(), page.getResult());

    }


//    /**
//     * 新增会员信息
//     * @param member
//     */
//    @Transactional
//    @Override
//    public void add(Member member) {
//        memberDao.add(member);
//    }

    /**
     * 根据id查询会员信息
     * @param id
     * @return
     */
    @Override
    public Member findById(int id) {
        return memberDao.findById(id);
    }




    /**
     * 查询所有会员档案
     *
     * @return
     */
    @Override
    public List<Member> findAll() {
        return memberDao.findAll();
    }

    /**
     * 编辑会员信息
     * @param member
     *
     * @Transactional 该注解给增删改方法添加事务
     */
    @Transactional
    @Override
    public void edit(Member member) {

        System.out.println(member.getId());
        memberDao.edit(member);
    }
}
